package com.company.oa.meeting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.common.utils.SecurityUtils;
import com.company.oa.meeting.dto.MeetingFeedbackDTO;
import com.company.oa.meeting.entity.MeetingFeedback;
import com.company.oa.meeting.service.MeetingFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 会议反馈控制器
 */
@RestController
@RequestMapping("/meeting-feedbacks")
public class MeetingFeedbackController {

    @Autowired
    private MeetingFeedbackService meetingFeedbackService;

    /**
     * 获取会议反馈列表
     */
    @GetMapping("/meeting/{meetingId}")
    public Result<IPage<MeetingFeedback>> getFeedbackPage(
            @PathVariable Long meetingId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        IPage<MeetingFeedback> pageResult = meetingFeedbackService.getFeedbackPage(page, size, meetingId);
        return Result.success(pageResult);
    }

    /**
     * 提交会议反馈
     */
    @PostMapping
    public Result<Boolean> submitFeedback(@RequestBody @Valid MeetingFeedbackDTO feedbackDTO) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        
        boolean result = meetingFeedbackService.submitFeedback(feedbackDTO, userId, userName);
        return Result.success(result);
    }

    /**
     * 获取会议反馈统计
     */
    @GetMapping("/statistics/{meetingId}")
    public Result<Map<String, Object>> getFeedbackStatistics(@PathVariable Long meetingId) {
        Map<String, Object> statistics = meetingFeedbackService.getFeedbackStatistics(meetingId);
        return Result.success(statistics);
    }

    /**
     * 检查用户是否已提交反馈
     */
    @GetMapping("/check/{meetingId}")
    public Result<Boolean> hasSubmittedFeedback(@PathVariable Long meetingId) {
        Long userId = SecurityUtils.getUserId();
        boolean hasSubmitted = meetingFeedbackService.hasSubmittedFeedback(meetingId, userId);
        return Result.success(hasSubmitted);
    }
}